//
//  AY_MaYunView.m
//  AiYue
//
//  Created by mac on 2023/11/16.
//

#import "AY_MaYunView.h"
#import "AY_BookVpPage.h"
#import "AY_BookDPage.h"

@interface AY_MaYunView ()

/** 背景按钮 */
@property (nonatomic, strong) UIView *boxView;

/**  */
@property (strong,nonatomic) UILabel *pLabel;

/**  */
@property (strong,nonatomic) UILabel *yLabel;

/** */
@property (strong,nonatomic) UILabel *autoInfoLabel;

/** */
@property (strong,nonatomic) UISwitch *autoSwitch;

/** */
@property (strong,nonatomic) UIButton *bButton;
@property (strong,nonatomic) UIButton *vButton;
@property (strong,nonatomic) UIView *infoView;
@property (strong,nonatomic) UILabel *infoLabel;

@property (strong,nonatomic) UIView *line1;
@property (strong,nonatomic) NSString *balance;
@property (strong,nonatomic) NSString *price;
@property (strong,nonatomic) NSString *bookId;
@end

@implementation AY_MaYunView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.boxView = [AYTool createViewWithFrame:self.bounds andBg:COLOR([AYMMKV getStringForKey:ReadToolBackColor defaultValue:@"FEF6E9"]) andRadius:0 andAddSubView:self];
        self.boxView.layer.masksToBounds = NO;
        self.boxView.layer.shadowColor = COLOR([AYMMKV getStringForKey:ReadToolBackColor defaultValue:@"FEF6E9"]).CGColor; // 阴影颜色
        self.boxView.layer.shadowOffset = CGSizeMake(0, -40); // 偏移距离
        self.boxView.layer.shadowOpacity = 1; // 不透明度
        self.boxView.layer.shadowRadius = 40; // 半径
        
        self.pLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR([AYMMKV getStringForKey:ReadTitleColor defaultValue:@"422D10"]) andBgColor:nil andFont:YFont(14) andLines:1 andRadius:0 andAddSubView:self.boxView];
        [self.pLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.boxView).offset(YNUM(15));
            make.top.equalTo(self.boxView).offset(YNUM(20));
        }];
        
        self.yLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR([AYMMKV getStringForKey:ReadTitleColor defaultValue:@"422D10"]) andBgColor:nil andFont:YFont(14) andLines:1 andRadius:0 andAddSubView:self.boxView];
        [self.yLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.boxView.mas_right).offset(-YNUM(15));
            make.centerY.equalTo(self.pLabel);
        }];
        
        self.line1 = [[UIView alloc] initWithFrame:CGRectMake(YNUM(15), YNUM(50), SCREEN_W - YNUM(30), 1)];
        [self.boxView addSubview:self.line1];
        [self drawLineOfDashByCAShapeLayer:self.line1 lineLength:self.line1.mj_w lineSpacing:YNUM(5) lineColor:COLOR([AYMMKV getStringForKey:ReadLineColor defaultValue:@"EDE5D8"]) lineDirection:YES];
        
        self.autoInfoLabel = [AYTool createLabelWithFrame:CGRectZero andText:[NSString stringWithFormat:@"自动%@下一章", [AYMMKV getStringForKey:GM]] andTextColor:COLOR([AYMMKV getStringForKey:ReadTitleColor defaultValue:@"422D10"]) andBgColor:nil andFont:YFont(14) andLines:1 andRadius:0 andAddSubView:self.boxView];
        [self.autoInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.pLabel);
            make.top.equalTo(self.line1.mas_bottom).offset(YNUM(15));
        }];
        
        self.autoSwitch = [[UISwitch alloc] init];
        self.autoSwitch.on = [AYMMKV getBoolForKey:AUTO defaultValue:YES];
        [self.autoSwitch addTarget:self action:@selector(autoSelecte:) forControlEvents:UIControlEventTouchUpInside];
        self.autoSwitch.onTintColor = THEME_COLOR;
        [self.boxView addSubview:self.autoSwitch];
        [self.autoSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.boxView.mas_right).offset(-YNUM(15));
            make.centerY.equalTo(self.autoInfoLabel);
        }];
        
        self.bButton = [AYTool createButtonWithFrame:CGRectZero andTitle:[NSString stringWithFormat:@"%@并%@", [AYMMKV getStringForKey:CZ], [AYMMKV getStringForKey:GM]] andFont:[UIFont boldSystemFontOfSize:YNUM(14)] andTitleColor:COLOR([AYMMKV getStringForKey:ReadTitleColor defaultValue:@"422D10"]) andBg:COLOR([AYMMKV getStringForKey:ReadLineColor defaultValue:@"EDE5D8"]) andImage:nil andBgImage:nil andTarget:self andAction:@selector(jumpQD) andRadius:YNUM(8) andAddSubView:self.boxView];
        [self.bButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.pLabel);
            make.right.equalTo(self.yLabel);
            make.top.equalTo(self.autoInfoLabel.mas_bottom).offset(YNUM(20));
            make.height.mas_equalTo(YNUM(50));
        }];
        
        self.vButton = [AYTool createButtonWithFrame:CGRectZero andTitle:[NSString stringWithFormat:@"  %@VIP全场免费阅读 >",[AYMMKV getStringForKey:KT]] andFont:[UIFont boldSystemFontOfSize:YNUM(14)] andTitleColor:THEME_COLOR andBg:COLOR([AYMMKV getStringForKey:ReadLineColor defaultValue:@"EDE5D8"]) andImage:YImage(@"vbImage") andBgImage:nil andTarget:self andAction:@selector(jumpVP) andRadius:YNUM(8) andAddSubView:self.boxView];
        [self.vButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.pLabel);
            make.right.equalTo(self.yLabel);
            make.top.equalTo(self.bButton.mas_bottom).offset(YNUM(20));
            make.height.mas_equalTo(YNUM(50));
        }];
        
        self.infoView = [AYTool createViewWithFrame:CGRectZero andBg:COLOR([AYMMKV getStringForKey:ReadToolBackColor defaultValue:@"FEF6E9"]) andRadius:YNUM(12) andAddSubView:self.vButton];
        [self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.vButton).offset(YNUM(10));
            make.top.equalTo(self.vButton).offset(-YNUM(10));
            make.width.mas_equalTo(YNUM(80));
            make.height.mas_equalTo(YNUM(35));
        }];
        
        self.infoLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"看书更省钱" andTextColor:[UIColor whiteColor] andBgColor:THEME_COLOR andFont:YFont(10) andLines:1 andRadius:0 andAddSubView:self.infoView];
        self.infoLabel.textAlignment = NSTextAlignmentCenter;
        [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.equalTo(self.infoView);
            make.top.right.equalTo(self.vButton);
        }];
    }
    return self;
}

#pragma mark 判断是否为整形
- (void)sendPrice:(NSString *)price andMyBalance:(NSString *)balance andBookId:(NSString *)bookId {
    self.balance = balance;
    self.bookId = bookId;
    self.price = price;
    self.pLabel.text = [self isPureInt:price] ? [NSString stringWithFormat:@"本章需要%@书豆",price] : [NSString stringWithFormat:@"本章需要%0.1f书豆",[price floatValue]];
    self.yLabel.text = [self isPureInt:balance] ? [NSString stringWithFormat:@"当前余额%@书豆",balance] : [NSString stringWithFormat:@"当前余额%0.1f书豆",[balance floatValue]];
    if ([balance floatValue] < [price floatValue]) {
        [self.bButton setTitle:[NSString stringWithFormat:@"%@并%@", [AYMMKV getStringForKey:CZ], [AYMMKV getStringForKey:GM]] forState:UIControlStateNormal];
    }else {
        [self.bButton setTitle:@"解锁本章" forState:UIControlStateNormal];
    }
}

- (BOOL)isPureInt:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}

- (void)jumpVP {
    AY_BookVpPage * vp = [[AY_BookVpPage alloc] init];
    vp.bookId = self.bookId;
    [[AYTool viewControllerWith:self].navigationController pushViewController:vp animated:YES];
}

- (void)jumpQD {
    if ([self.balance floatValue] < [self.price floatValue]) {
        AY_BookDPage * vc = [[AY_BookDPage alloc] init];
        vc.bookId = self.bookId;
        [[AYTool viewControllerWith:self].navigationController pushViewController:vc animated:YES];
    }else {
        [[NSNotificationCenter defaultCenter] postNotificationName:BOOKDOUGM object:nil];
    }
}

- (void)updateColors {
    NSString *color = [AYMMKV getStringForKey:ReadTitleColor defaultValue:@"422D10"];
    self.boxView.backgroundColor = COLOR([AYMMKV getStringForKey:ReadToolBackColor defaultValue:@"FEF6E9"]);
    self.boxView.layer.shadowColor = COLOR([AYMMKV getStringForKey:ReadToolBackColor defaultValue:@"FEF6E9"]).CGColor;
    self.pLabel.textColor = COLOR([AYMMKV getStringForKey:ReadTitleColor defaultValue:@"422D10"]);
    self.yLabel.textColor = COLOR([AYMMKV getStringForKey:ReadTitleColor defaultValue:@"422D10"]);
    [self drawLineOfDashByCAShapeLayer:self.line1 lineLength:self.line1.mj_w lineSpacing:YNUM(5) lineColor:COLOR([AYMMKV getStringForKey:ReadLineColor defaultValue:@"EDE5D8"]) lineDirection:YES];
    self.autoInfoLabel.textColor = COLOR([AYMMKV getStringForKey:ReadTitleColor defaultValue:@"422D10"]);
    [self.bButton setTitleColor:[color isEqual:@"999999"] ? [[UIColor whiteColor] colorWithAlphaComponent:0.8f] : COLOR([AYMMKV getStringForKey:ReadTitleColor defaultValue:@"422D10"])  forState:UIControlStateNormal];
    self.bButton.backgroundColor = COLOR([AYMMKV getStringForKey:ReadLineColor defaultValue:@"EDE5D8"]);
//    [self.vButton setTitleColor:COLOR([AYMMKV getStringForKey:ReadTitleColor defaultValue:@"422D10"]) forState:UIControlStateNormal];
    self.vButton.backgroundColor = COLOR([AYMMKV getStringForKey:ReadLineColor defaultValue:@"EDE5D8"]);
    self.infoView.backgroundColor = COLOR([AYMMKV getStringForKey:ReadToolBackColor defaultValue:@"FEF6E9"]);
}

- (void)autoSelecte:(UISwitch *)autoSwitch {
    [AYMMKV setBool:autoSwitch.on forKey:AUTO];
}

/**
 *  通过 CAShapeLayer 方式绘制虚线
 *
 *  param lineView:       需要绘制成虚线的view
 *  param lineLength:     虚线的宽度
 *  param lineSpacing:    虚线的间距
 *  param lineColor:      虚线的颜色
 *  param lineDirection   虚线的方向  YES 为水平方向， NO 为垂直方向
 **/
- (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor lineDirection:(BOOL)isHorizonal {
  
    CAShapeLayer *dotteShapeLayer = [CAShapeLayer layer];

    CGMutablePathRef dotteShapePath =  CGPathCreateMutable();

    //设置虚线颜色为blackColor
    [dotteShapeLayer setStrokeColor:[lineColor CGColor]];

    //设置虚线宽度
    dotteShapeLayer.lineWidth = 1.0f ;

    //10=线的宽度 5=每条线的间距
    NSArray *dotteShapeArr = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:5],[NSNumber numberWithInt:3], nil];

    [dotteShapeLayer setLineDashPattern:dotteShapeArr];

    // 50为虚线Y值，和下面的50一起用。
    // kScreenWidth为虚线宽度
    CGPathMoveToPoint(dotteShapePath, NULL, 0 ,0);

    CGPathAddLineToPoint(dotteShapePath, NULL, CGRectGetWidth(lineView.frame), 0);

    [dotteShapeLayer setPath:dotteShapePath];

    CGPathRelease(dotteShapePath);

    //把绘制好的虚线添加上来
    [lineView.layer addSublayer:dotteShapeLayer];
}
@end
